package jmine.tec.web.front.forms;

import static java.util.Collections.singleton;
import jmine.tec.extensions.forms.FormHandler;
import jmine.tec.web.front.FrontException;
import jmine.tec.web.front.FrontMessages;
import jmine.tec.web.front.dispatcher.request.BackOfficeRequestFiller;
import jmine.tec.web.front.pages.form.HandledFormPanelFactory;
import jmine.tec.web.front.pages.requests.filter.BackOfficePropertyPanelFactory;

import org.springframework.beans.factory.InitializingBean;

/**
 * Form metadata
 * 
 * @author lundberg
 * @param <F> form type
 */
public class FormMetadata<F> implements InitializingBean {

    private final Class<F> formClass;

    private FormHandler<F> formHandler;

    private BackOfficeRequestFiller<F> customFiller;

    private FormMetadataRegistry formMetadataRegistry;

    private HandledFormPanelFactory formPanelFactory;

    private BackOfficePropertyPanelFactory propertyPanelFactory;

    /**
     * Constructor
     * 
     * @param formClass formClass
     */
    public FormMetadata(Class<F> formClass) {
        super();
        this.formClass = formClass;
    }

    /**
     * Constructor
     * 
     * @param formClass formClass
     * @param formHandler formHandler
     * @param customFiller customFiller
     */
    public FormMetadata(Class<F> formClass, FormHandler<F> formHandler, BackOfficeRequestFiller<F> customFiller) {
        super();
        this.formClass = formClass;
        this.formHandler = formHandler;
        this.customFiller = customFiller;
    }

    /**
     * @return the formClass
     */
    public Class<F> getFormClass() {
        return this.formClass;
    }

    /**
     * @return the formHandler
     */
    public FormHandler<F> getFormHandler() {
        if (this.formHandler == null) {
            throw new FrontException(FrontMessages.NO_HANDLER_FOUND_FOR_FORM.create());
        }
        return this.formHandler;
    }

    /**
     * @return the customFiller
     */
    public BackOfficeRequestFiller<F> getCustomFiller() {
        return this.customFiller;
    }

    /**
     * @param formHandler the formHandler to set
     */
    public void setFormHandler(FormHandler<F> formHandler) {
        this.formHandler = formHandler;
    }

    /**
     * @param customFiller the customFiller to set
     */
    public void setCustomFiller(BackOfficeRequestFiller<F> customFiller) {
        this.customFiller = customFiller;
    }

    /**
     * @param formMetadataRegistry the registry to set
     */
    public void setFormMetadataRegistry(FormMetadataRegistry formMetadataRegistry) {
        this.formMetadataRegistry = formMetadataRegistry;
    }

    /**
     * {@inheritDoc}
     */
    public void afterPropertiesSet() throws Exception {
        this.formMetadataRegistry.register(singleton(this));
    }

    /**
     * @return the formPanelFactory
     */
    public HandledFormPanelFactory getFormPanelFactory() {
        return this.formPanelFactory;
    }

    /**
     * @param formPanelFactory the formPanelFactory to set
     */
    public void setFormPanelFactory(HandledFormPanelFactory formPanelFactory) {
        this.formPanelFactory = formPanelFactory;
    }

    /**
     * @return property panel factory
     */
    public BackOfficePropertyPanelFactory getPropertyPanelFactory() {
        return this.propertyPanelFactory;
    }

    /**
     * @param propertyPanelFactory the propertyPanelFactory to set
     */
    public void setPropertyPanelFactory(BackOfficePropertyPanelFactory propertyPanelFactory) {
        this.propertyPanelFactory = propertyPanelFactory;
    }
}
